home *** CD-ROM | disk | FTP | other *** search
/ AGA Toolkit '97 / The AGA Toolkit '97.iso / miscellaneous / science / maths / calc / source / zmath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-07  |  30.0 KB  |  1,723 lines

  1. /*
  2.  * Copyright (c) 1994 David I. Bell
  3.  * Permission is granted to use, distribute, or modify this source,
  4.  * provided that this copyright notice remains intact.
  5.  *
  6.  * Extended precision integral arithmetic primitives
  7.  */
  8.  
  9. #include "zmath.h"
  10.  
  11.  
  12. HALF _twoval_[] = { 2 };
  13. HALF _zeroval_[] = { 0 };
  14. HALF _oneval_[] = { 1 };
  15. HALF _tenval_[] = { 10 };
  16.  
  17. ZVALUE _zero_ = { _zeroval_, 1, 0};
  18. ZVALUE _one_ = { _oneval_, 1, 0 };
  19. ZVALUE _ten_ = { _tenval_, 1, 0 };
  20.  
  21.  
  22. /*
  23.  * mask of given bits, rotated thru all bit positions twice
  24.  *
  25.  * bitmask[i]      (1 << (i-1)),  for  -BASEB*4<=i<=BASEB*4
  26.  */
  27. static HALF *bmask;        /* actual rotation thru 8 cycles */
  28. static HALF **rmask;        /* actual rotation pointers thru 2 cycles */
  29. HALF *bitmask;            /* bit rotation, norm 0 */
  30.  
  31. BOOL _math_abort_;        /* nonzero to abort calculations */
  32.  
  33.  
  34. static void dadd MATH_PROTO((ZVALUE z1, ZVALUE z2, long y, long n));
  35. static BOOL dsub MATH_PROTO((ZVALUE z1, ZVALUE z2, long y, long n));
  36. static void dmul MATH_PROTO((ZVALUE z, FULL x, ZVALUE *dest));
  37.  
  38.  
  39. #ifdef ALLOCTEST
  40. static long nalloc = 0;
  41. static long nfree = 0;
  42. #endif
  43.  
  44.  
  45. HALF *
  46. alloc(len)
  47.     long len;
  48. {
  49.     HALF *hp;
  50.  
  51.     if (_math_abort_)
  52.         math_error("Calculation aborted");
  53.     hp = (HALF *) malloc((len+1) * sizeof(HALF));
  54.     if (hp == 0)
  55.         math_error("Not enough memory");
  56. #ifdef ALLOCTEST
  57.     ++nalloc;
  58. #endif
  59.     return hp;
  60. }
  61.  
  62.  
  63. #ifdef ALLOCTEST
  64. void
  65. freeh(h)
  66.     HALF *h;
  67. {
  68.     if ((h != _zeroval_) && (h != _oneval_)) {
  69.         free(h);
  70.         ++nfree;
  71.     }
  72. }
  73.  
  74.  
  75. void
  76. allocStat()
  77. {
  78.     fprintf(stderr, "nalloc: %ld nfree: %ld kept: %ld\n",
  79.         nalloc, nfree, nalloc - nfree);
  80. }
  81. #endif
  82.  
  83.  
  84. /*
  85.  * Convert a normal integer to a number.
  86.  */
  87. void
  88. itoz(i, res)
  89.     long i;
  90.     ZVALUE *res;
  91. {
  92.     long diddle, len;
  93.  
  94.     res->len = 1;
  95.     res->sign = 0;
  96.     diddle = 0;
  97.     if (i == 0) {
  98.         res->v = _zeroval_;
  99.         return;
  100.     }
  101.     if (i < 0) {
  102.         res->sign = 1;
  103.         i = -i;
  104.         if (i < 0) {    /* fix most negative number */
  105.             diddle = 1;
  106.             i--;
  107.         }
  108.     }
  109.     if (i == 1) {
  110.         res->v = _oneval_;
  111.         return;
  112.     }
  113.     len = 1 + (((FULL) i) >= BASE);
  114.     res->len = len;
  115.     res->v = alloc(len);
  116.     res->v[0] = (HALF) (i + diddle);
  117.     if (len == 2)
  118.         res->v[1] = (HALF) (i / BASE);
  119. }
  120.  
  121.  
  122. /*
  123.  * Convert a number to a normal integer, as far as possible.
  124.  * If the number is out of range, the largest number is returned.
  125.  */
  126. long
  127. ztoi(z)
  128.     ZVALUE z;
  129. {
  130.     long i;
  131.  
  132.     if (zgtmaxfull(z)) {
  133.         i = MAXFULL;
  134.         return (z.sign ? -i : i);
  135.     }
  136.     i = (zistiny(z) ? z1tol(z) : z2tol(z));
  137.     return (z.sign ? -i : i);
  138. }
  139.  
  140.  
  141. /*
  142.  * Convert a normal unsigned integer to a number.
  143.  */
  144. void
  145. utoz(i, res)
  146.     FULL i;
  147.     ZVALUE *res;
  148. {
  149.     long len;
  150.  
  151.     res->len = 1;
  152.     res->sign = 0;
  153.     if (i == 0) {
  154.         res->v = _zeroval_;
  155.         return;
  156.     }
  157.     if (i == 1) {
  158.         res->v = _oneval_;
  159.         return;
  160.     }
  161.     len = 1 + (((FULL) i) >= BASE);
  162.     res->len = len;
  163.     res->v = alloc(len);
  164.     res->v[0] = (HALF)i;
  165.     if (len == 2)
  166.         res->v[1] = (HALF) (i / BASE);
  167. }
  168.  
  169.  
  170. /*
  171.  * Convert a number to a unsigned normal integer, as far as possible.
  172.  * If the number is out of range, the largest number is returned.
  173.  * The absolute value of z is converted.
  174.  */
  175. FULL
  176. ztou(z)
  177.     ZVALUE z;
  178. {
  179.     if (z.len > 2) {
  180.         return MAXUFULL;
  181.     }
  182.     return ztofull(z);
  183. }
  184.  
  185.  
  186. /*
  187.  * Make a copy of an integer value
  188.  */
  189. void
  190. zcopy(z, res)
  191.     ZVALUE z, *res;
  192. {
  193.     res->sign = z.sign;
  194.     res->len = z.len;
  195.     if (zisleone(z)) {    /* zero or plus or minus one are easy */
  196.         res->v = (z.v[0] ? _oneval_ : _zeroval_);
  197.         return;
  198.     }
  199.     res->v = alloc(z.len);
  200.     zcopyval(z, *res);
  201. }
  202.  
  203.  
  204. /*
  205.  * Add together two integers.
  206.  */
  207. void
  208. zadd(z1, z2, res)
  209.     ZVALUE z1, z2, *res;
  210. {
  211.     ZVALUE dest;
  212.     HALF *p1, *p2, *pd;
  213.     long len;
  214.     FULL carry;
  215.     SIUNION sival;
  216.  
  217.     if (z1.sign && !z2.sign) {
  218.         z1.sign = 0;
  219.         zsub(z2, z1, res);
  220.         return;
  221.     }
  222.     if (z2.sign && !z1.sign) {
  223.         z2.sign = 0;
  224.         zsub(z1, z2, res);
  225.         return;
  226.     }
  227.     if (z2.len > z1.len) {
  228.         pd = z1.v; z1.v = z2.v; z2.v = pd;
  229.         len = z1.len; z1.len = z2.len; z2.len = len;
  230.     }
  231.     dest.len = z1.len + 1;
  232.     dest.v = alloc(dest.len);
  233.     dest.sign = z1.sign;
  234.     carry = 0;
  235.     pd = dest.v;
  236.     p1 = z1.v;
  237.     p2 = z2.v;
  238.     len = z2.len;
  239.     while (len--) {
  240.         sival.ivalue = ((FULL) *p1++) + ((FULL) *p2++) + carry;
  241.         *pd++ = sival.silow;
  242.         carry = sival.sihigh;
  243.     }
  244.     len = z1.len - z2.len;
  245.     while (len--) {
  246.         sival.ivalue = ((FULL) *p1++) + carry;
  247.         *pd++ = sival.silow;
  248.         carry = sival.sihigh;
  249.     }
  250.     *pd = (HALF)carry;
  251.     zquicktrim(dest);
  252.     *res = dest;
  253. }
  254.  
  255.  
  256. /*
  257.  * Subtract two integers.
  258.  */
  259. void
  260. zsub(z1, z2, res)
  261.     ZVALUE z1, z2, *res;
  262. {
  263.     register HALF *h1, *h2, *hd;
  264.     long len1, len2;
  265.     FULL carry;
  266.     SIUNION sival;
  267.     ZVALUE dest;
  268.  
  269.     if (z1.sign != z2.sign) {
  270.         z2.sign = z1.sign;
  271.         zadd(z1, z2, res);
  272.         return;
  273.     }
  274.     len1 = z1.len;
  275.     len2 = z2.len;
  276.     if (len1 == len2) {
  277.         h1 = z1.v + len1 - 1;
  278.         h2 = z2.v + len2 - 1;
  279.         while ((len1 > 0) && ((FULL)*h1 == (FULL)*h2)) {
  280.             len1--;
  281.             h1--;
  282.             h2--;
  283.         }
  284.         if (len1 == 0) {
  285.             *res = _zero_;
  286.             return;
  287.         }
  288.         len2 = len1;
  289.         carry = ((FULL)*h1 < (FULL)*h2);
  290.     } else {
  291.         carry = (len1 < len2);
  292.     }
  293.     dest.sign = z1.sign;
  294.     h1 = z1.v;
  295.     h2 = z2.v;
  296.     if (carry) {
  297.         carry = len1;
  298.         len1 = len2;
  299.         len2 = carry;
  300.         h1 = z2.v;
  301.         h2 = z1.v;
  302.         dest.sign = !dest.sign;
  303.     }
  304.     hd = alloc(len1);
  305.     dest.v = hd;
  306.     dest.len = len1;
  307.     len1 -= len2;
  308.     carry = 0;
  309.     while (--len2 >= 0) {
  310.         sival.ivalue = (BASE1 - ((FULL) *h1++)) + *h2++ + carry;
  311.         *hd++ = BASE1 - sival.silow;
  312.         carry = sival.sihigh;
  313.     }
  314.     while (--len1 >= 0) {
  315.         sival.ivalue = (BASE1 - ((FULL) *h1++)) + carry;
  316.         *hd++ = BASE1 - sival.silow;
  317.         carry = sival.sihigh;
  318.     }
  319.     if (hd[-1] == 0)
  320.         ztrim(&dest);
  321.     *res = dest;
  322. }
  323.  
  324.  
  325. /*
  326.  * Multiply an integer by a small number.
  327.  */
  328. void
  329. zmuli(z, n, res)
  330.     ZVALUE z;
  331.     long n;
  332.     ZVALUE *res;
  333. {
  334.     register HALF *h1, *sd;
  335.     FULL low;
  336.     FULL high;
  337.     FULL carry;
  338.     long len;
  339.     SIUNION sival;
  340.     ZVALUE dest;
  341.  
  342.     if ((n == 0) || ziszero(z)) {
  343.         *res = _zero_;
  344.         return;
  345.     }
  346.     if (n < 0) {
  347.         n = -n;
  348.         z.sign = !z.sign;
  349.     }
  350.     if (n == 1) {
  351.         zcopy(z, res);
  352.         return;
  353.     }
  354.     low = ((FULL) n) & BASE1;
  355.     high = ((FULL) n) >> BASEB;
  356.     dest.len = z.len + 2;
  357.     dest.v = alloc(dest.len);
  358.     dest.sign = z.sign;
  359.     /*
  360.      * Multiply by the low digit.
  361.      */
  362.     h1 = z.v;
  363.     sd = dest.v;
  364.     len = z.len;
  365.     carry = 0;
  366.     while (len--) {
  367.         sival.ivalue = ((FULL) *h1++) * low + carry;
  368.         *sd++ = sival.silow;
  369.         carry = sival.sihigh;
  370.     }
  371.     *sd = (HALF)carry;
  372.     /*
  373.      * If there was only one digit, then we are all done except
  374.      * for trimming the number if there was no last carry.
  375.      */
  376.     if (high == 0) {
  377.         dest.len--;
  378.         if (carry == 0)
  379.             dest.len--;
  380.         *res = dest;
  381.         return;
  382.     }
  383.     /*
  384.      * Need to multiply by the high digit and add it into the
  385.      * previous value.  Clear the final word of rubbish first.
  386.      */
  387.     *(++sd) = 0;
  388.     h1 = z.v;
  389.     sd = dest.v + 1;
  390.     len = z.len;
  391.     carry = 0;
  392.     while (len--) {
  393.         sival.ivalue = ((FULL) *h1++) * high + ((FULL) *sd) + carry;
  394.         *sd++ = sival.silow;
  395.         carry = sival.sihigh;
  396.     }
  397.     *sd = (HALF)carry;
  398.     zquicktrim(dest);
  399.     *res = dest;
  400. }
  401.  
  402.  
  403. /*
  404.  * Divide two numbers by their greatest common divisor.
  405.  * This is useful for reducing the numerator and denominator of
  406.  * a fraction to its lowest terms.
  407.  */
  408. void
  409. zreduce(z1, z2, z1res, z2res)
  410.     ZVALUE z1, z2, *z1res, *z2res;
  411. {
  412.     ZVALUE tmp;
  413.  
  414.     if (zisleone(z1) || zisleone(z2))
  415.         tmp = _one_;
  416.     else
  417.         zgcd(z1, z2, &tmp);
  418.     if (zisunit(tmp)) {
  419.         zcopy(z1, z1res);
  420.         zcopy(z2, z2res);
  421.     } else {
  422.         zquo(z1, tmp, z1res);
  423.         zquo(z2, tmp, z2res);
  424.     }
  425.     zfree(tmp);
  426. }
  427.  
  428.  
  429. /*
  430.  * Divide two numbers to obtain a quotient and remainder.
  431.  * This algorithm is taken from
  432.  * Knuth, The Art of Computer Programming, vol 2: Seminumerical Algorithms.
  433.  * Slight modifications were made to speed this mess up.
  434.  */
  435. void
  436. zdiv(z1, z2, res, rem)
  437.     ZVALUE z1, z2, *res, *rem;
  438. {
  439.     long i, j, k;
  440.     register HALF *q, *pp;
  441.     SIUNION pair;        /* pair of halfword values */
  442.     HALF h2, v2;
  443.     long y;
  444.     FULL x;
  445.     ZVALUE ztmp1, ztmp2, ztmp3, quo;
  446.  
  447.     if (ziszero(z2))
  448.         math_error("Division by zero");
  449.     if (ziszero(z1)) {
  450.         *res = _zero_;
  451.         *rem = _zero_;
  452.         return;
  453.     }
  454.     if (zisone(z2)) {
  455.         zcopy(z1, res);
  456.         *rem = _zero_;
  457.         return;
  458.     }
  459.     i = BASE / 2;
  460.     j = 0;
  461.     k = (long) z2.v[z2.len - 1];
  462.     while (! (k & i)) {
  463.         j ++;
  464.         i >>= 1;
  465.     }
  466.     ztmp1.v = alloc(z1.len + 1);
  467.     ztmp1.len = z1.len + 1;
  468.     zcopyval(z1, ztmp1);
  469.     ztmp1.v[z1.len] = 0;
  470.     ztmp1.sign = 0;
  471.     ztmp2.v = alloc(z2.len);
  472.     ztmp2.len = z2.len;
  473.     ztmp2.sign = 0;
  474.     zcopyval(z2, ztmp2);
  475.     if (zrel(ztmp1, ztmp2) < 0) {
  476.         rem->v = ztmp1.v;
  477.         rem->sign = 0;
  478.         rem->len = z1.len;
  479.         zfree(ztmp2);
  480.         *res = _zero_;
  481.         return;
  482.     }
  483.     quo.len = z1.len - z2.len + 1;
  484.     quo.v = alloc(quo.len);
  485.     quo.sign = z1.sign != z2.sign;
  486.     zclearval(quo);
  487.  
  488.     ztmp3.v = zalloctemp(z2.len + 1);
  489.  
  490.     /*
  491.      * Normalize z1 and z2
  492.      */
  493.     zshiftl(ztmp1, j);
  494.     zshiftl(ztmp2, j);
  495.  
  496.     k = ztmp1.len - ztmp2.len;
  497.     q = quo.v + quo.len;
  498.     y = ztmp1.len - 1;
  499.     h2 = ztmp2.v [ztmp2.len - 1];
  500.     v2 = 0;
  501.     if (ztmp2.len >= 2)
  502.         v2 = ztmp2.v [ztmp2.len - 2];
  503.     for (;k--; --y) {
  504.         pp = ztmp1.v + y - 1;
  505.         pair.silow = pp[0];
  506.         pair.sihigh = pp[1];
  507.         if (ztmp1.v[y] == h2)
  508.             x = BASE1;
  509.         else
  510.             x = pair.ivalue / h2;
  511.         if (x) {
  512.             while (pair.ivalue - x * h2 < BASE && y > 1 &&
  513.                 v2 * x > (pair.ivalue - x * h2) * BASE + ztmp1.v [y-2]) {
  514.                     --x;
  515.             }
  516.             dmul(ztmp2, x, &ztmp3);
  517. #ifdef divblab
  518.             printf(" x: %ld\n", x);
  519.             printf("ztmp1: ");
  520.             printz(ztmp1);
  521.             printf("ztmp2: ");
  522.             printz(ztmp2);
  523.             printf("ztmp3: ");
  524.             printz(ztmp3);
  525. #endif
  526.             if (dsub(ztmp1, ztmp3, y, ztmp2.len)) {
  527.                 --x;
  528.                 /*
  529.                 printf("adding back\n");
  530.                 */
  531.                 dadd(ztmp1, ztmp2, y, ztmp2.len);
  532.             }
  533.         }
  534.         ztrim(&ztmp1);
  535.         *--q = (HALF)x;
  536.     }
  537.     zshiftr(ztmp1, j);
  538.     *rem = ztmp1;
  539.     ztrim(rem);
  540.     zfree(ztmp2);
  541.     ztrim(&quo);
  542.     *res = quo;
  543. }
  544.  
  545.  
  546. /*
  547.  * Return the quotient and remainder of an integer divided by a small
  548.  * number.  A nonzero remainder is only meaningful when both numbers
  549.  * are positive.
  550.  */
  551. long
  552. zdivi(z, n, res)
  553.     ZVALUE z, *res;
  554.     long n;
  555. {
  556.     register HALF *h1, *sd;
  557.     FULL val;
  558.     HALF divval[2];
  559.     ZVALUE div;
  560.     ZVALUE dest;
  561.     long len;
  562.  
  563.     if (n == 0)
  564.         math_error("Division by zero");
  565.     if (ziszero(z)) {
  566.         *res = _zero_;
  567.         return 0;
  568.     }
  569.     if (n < 0) {
  570.         n = -n;
  571.         z.sign = !z.sign;
  572.     }
  573.     if (n == 1) {
  574.         zcopy(z, res);
  575.         return 0;
  576.     }
  577.     /*
  578.      * If the division is by a large number, then call the normal
  579.      * divide routine.
  580.      */
  581.     if (n & ~BASE1) {
  582.         div.sign = 0;
  583.         div.len = 2;
  584.         div.v = divval;
  585.         divval[0] = (HALF) n;
  586.         divval[1] = ((FULL) n) >> BASEB;
  587.         zdiv(z, div, res, &dest);
  588.         n = (zistiny(dest) ? z1tol(dest) : z2tol(dest));
  589.         zfree(dest);
  590.         return n;
  591.     }
  592.     /*
  593.      * Division is by a small number, so we can be quick about it.
  594.      */
  595.     len = z.len;
  596.     dest.sign = z.sign;
  597.     dest.len = len;
  598.     dest.v = alloc(len);
  599.     h1 = z.v + len - 1;
  600.     sd = dest.v + len - 1;
  601.     val = 0;
  602.     while (len--) {
  603.         val = ((val << BASEB) + ((FULL) *h1--));
  604.         *sd-- = val / n;
  605.         val %= n;
  606.     }
  607.     zquicktrim(dest);
  608.     *res = dest;
  609.     return val;
  610. }
  611.  
  612.  
  613. /*
  614.  * Return the quotient of two numbers.
  615.  * This works the same as zdiv, except that the remainer is not returned.
  616.  */
  617. void
  618. zquo(z1, z2, res)
  619.     ZVALUE z1, z2, *res;
  620. {
  621.     long i, j, k;
  622.     register HALF *q, *pp;
  623.     SIUNION pair;            /* pair of halfword values */
  624.     HALF h2, v2;
  625.     long y;
  626.     FULL x;
  627.     ZVALUE ztmp1, ztmp2, ztmp3, quo;
  628.  
  629.     if (ziszero(z2))
  630.         math_error("Division by zero");
  631.     if (ziszero(z1)) {
  632.         *res = _zero_;
  633.         return;
  634.     }
  635.     if (zisone(z2)) {
  636.         zcopy(z1, res);
  637.         return;
  638.     }
  639.     i = BASE / 2;
  640.     j = 0;
  641.     k = (long) z2.v[z2.len - 1];
  642.     while (! (k & i)) {
  643.         j ++;
  644.         i >>= 1;
  645.     }
  646.     ztmp1.v = alloc(z1.len + 1);
  647.     ztmp1.len = z1.len + 1;
  648.     zcopyval(z1, ztmp1);
  649.     ztmp1.v[z1.len] = 0;
  650.     ztmp1.sign = 0;
  651.     ztmp2.v = alloc(z2.len);
  652.     ztmp2.len = z2.len;
  653.     ztmp2.sign = 0;
  654.     zcopyval(z2, ztmp2);
  655.     if (zrel(ztmp1, ztmp2) < 0) {
  656.         zfree(ztmp1);
  657.         zfree(ztmp2);
  658.         *res = _zero_;
  659.         return;
  660.     }
  661.     quo.len = z1.len - z2.len + 1;
  662.     quo.v = alloc(quo.len);
  663.     quo.sign = z1.sign != z2.sign;
  664.     zclearval(quo);
  665.  
  666.     ztmp3.v = zalloctemp(z2.len + 1);
  667.  
  668.     /*
  669.      * Normalize z1 and z2
  670.      */
  671.     zshiftl(ztmp1, j);
  672.     zshiftl(ztmp2, j);
  673.  
  674.     k = ztmp1.len - ztmp2.len;
  675.     q = quo.v + quo.len;
  676.     y = ztmp1.len - 1;
  677.     h2 = ztmp2.v [ztmp2.len - 1];
  678.     v2 = 0;
  679.     if (ztmp2.len >= 2)
  680.         v2 = ztmp2.v [ztmp2.len - 2];
  681.     for (;k--; --y) {
  682.         pp = ztmp1.v + y - 1;
  683.         pair.silow = pp[0];
  684.         pair.sihigh = pp[1];
  685.         if (ztmp1.v[y] == h2)
  686.             x = BASE1;
  687.         else
  688.             x = pair.ivalue / h2;
  689.         if (x) {
  690.             while (pair.ivalue - x * h2 < BASE && y > 1 &&
  691.                 v2 * x > (pair.ivalue - x * h2) * BASE + ztmp1.v [y-2]) {
  692.                     --x;
  693.             }
  694.             dmul(ztmp2, x, &ztmp3);
  695.             if (dsub(ztmp1, ztmp3, y, ztmp2.len)) {
  696.                 --x;
  697.                 dadd(ztmp1, ztmp2, y, ztmp2.len);
  698.             }
  699.         }
  700.         ztrim(&ztmp1);
  701.         *--q = (HALF)x;
  702.     }
  703.     zfree(ztmp1);
  704.     zfree(ztmp2);
  705.     ztrim(&quo);
  706.     *res = quo;
  707. }
  708.  
  709.  
  710. /*
  711.  * Compute the remainder after dividing one number by another.
  712.  * This is only defined for positive z2 values.
  713.  * The result is normalized to lie in the range 0 to z2-1.
  714.  */
  715. void
  716. zmod(z1, z2, rem)
  717.     ZVALUE z1, z2, *rem;
  718. {
  719.     long i, j, k, neg;
  720.     register HALF *pp;
  721.     SIUNION pair;            /* pair of halfword values */
  722.     HALF h2, v2;
  723.     long y;
  724.     FULL x;
  725.     ZVALUE ztmp1, ztmp2, ztmp3;
  726.  
  727.     if (ziszero(z2))
  728.         math_error("Division by zero");
  729.     if (zisneg(z2))
  730.         math_error("Non-positive modulus");
  731.     if (ziszero(z1) || zisunit(z2)) {
  732.         *rem = _zero_;
  733.         return;
  734.     }
  735.     if (zistwo(z2)) {
  736.         if (zisodd(z1))
  737.             *rem = _one_;
  738.         else
  739.             *rem = _zero_;
  740.         return;
  741.     }
  742.     neg = z1.sign;
  743.     z1.sign = 0;
  744.  
  745.     /*
  746.      * Do a quick check to see if the absolute value of the number
  747.      * is less than the modulus.  If so, then the result is just a
  748.      * subtract or a copy.
  749.      */
  750.     h2 = z1.v[z1.len - 1];
  751.     v2 = z2.v[z2.len - 1];
  752.     if ((z1.len < z2.len) || ((z1.len == z2.len) && (h2 < v2))) {
  753.         if (neg)
  754.             zsub(z2, z1, rem);
  755.         else
  756.             zcopy(z1, rem);
  757.         return;
  758.     }
  759.  
  760.     /*
  761.      * Do another quick check to see if the number is positive and
  762.      * between the size of the modulus and twice the modulus.
  763.      * If so, then the answer is just another subtract.
  764.      */
  765.     if (!neg && (z1.len == z2.len) && (h2 > v2) &&
  766.         (((FULL) h2) < 2 * ((FULL) v2)))
  767.     {
  768.         zsub(z1, z2, rem);
  769.         return;
  770.     }
  771.  
  772.     /*
  773.      * If the modulus is an exact power of two, then the result
  774.      * can be obtained by ignoring the high bits of the number.
  775.      * This truncation assumes that the number of words for the
  776.      * number is at least as large as the number of words in the
  777.      * modulus, which is true at this point.
  778.      */
  779.     if (((v2 & -v2) == v2) && zisonebit(z2)) {    /* ASSUMES 2'S COMP */
  780.         i = zhighbit(z2);
  781.         z1.len = (i + BASEB - 1) / BASEB;
  782.         zcopy(z1, &ztmp1);
  783.         i %= BASEB;
  784.         if (i)
  785.             ztmp1.v[ztmp1.len - 1] &= ((((HALF) 1) << i) - 1);
  786.         ztmp2.len = 0;
  787.         goto gotanswer;
  788.     }
  789.  
  790.     /*
  791.      * If the modulus is one less than an exact power of two, then
  792.      * the result can be simplified similarly to "casting out 9's".
  793.      * Only do this simplification for large enough modulos.
  794.      */
  795.     if ((z2.len > 1) && (z2.v[0] == BASE1) && zisallbits(z2)) {
  796.         i = -(zhighbit(z2) + 1);
  797.         zcopy(z1, &ztmp1);
  798.         z1 = ztmp1;
  799.         while ((k = zrel(z1, z2)) > 0) {
  800.             ztmp1 = _zero_;
  801.             while (!ziszero(z1)) {
  802.                 zand(z1, z2, &ztmp2);
  803.                 zadd(ztmp2, ztmp1, &ztmp3);
  804.                 zfree(ztmp1);
  805.                 zfree(ztmp2);
  806.                 ztmp1 = ztmp3;
  807.                 zshift(z1, i, &ztmp2);
  808.                 zfree(z1);
  809.                 z1 = ztmp2;
  810.             }
  811.             zfree(z1);
  812.             z1 = ztmp1;
  813.         }
  814.         if (k == 0) {
  815.             zfree(ztmp1);
  816.             *rem = _zero_;
  817.             return;
  818.         }
  819.         ztmp2.len = 0;
  820.         goto gotanswer;
  821.     }
  822.  
  823.     /*
  824.      * Must actually do the divide.
  825.      */
  826.     i = BASE / 2;
  827.     j = 0;
  828.     k = (long) z2.v[z2.len - 1];
  829.     while (! (k & i)) {
  830.         j ++;
  831.         i >>= 1;
  832.     }
  833.     ztmp1.v = alloc(z1.len + 1);
  834.     ztmp1.len = z1.len + 1;
  835.     zcopyval(z1, ztmp1);
  836.     ztmp1.v[z1.len] = 0;
  837.     ztmp1.sign = 0;
  838.     ztmp2.v = alloc(z2.len);
  839.     ztmp2.len = z2.len;
  840.     ztmp2.sign = 0;
  841.     zcopyval(z2, ztmp2);
  842.     if (zrel(ztmp1, ztmp2) < 0)
  843.         goto gotanswer;
  844.  
  845.     ztmp3.v = zalloctemp(z2.len + 1);
  846.  
  847.     /*
  848.      * Normalize z1 and z2
  849.      */
  850.     zshiftl(ztmp1, j);
  851.     zshiftl(ztmp2, j);
  852.  
  853.     k = ztmp1.len - ztmp2.len;
  854.     y = ztmp1.len - 1;
  855.     h2 = ztmp2.v [ztmp2.len - 1];
  856.     v2 = 0;
  857.     if (ztmp2.len >= 2)
  858.         v2 = ztmp2.v [ztmp2.len - 2];
  859.     for (;k--; --y) {
  860.         pp = ztmp1.v + y - 1;
  861.         pair.silow = pp[0];
  862.         pair.sihigh = pp[1];
  863.         if (ztmp1.v[y] == h2)
  864.             x = BASE1;
  865.         else
  866.             x = pair.ivalue / h2;
  867.         if (x) {
  868.             while (pair.ivalue - x * h2 < BASE && y > 1 &&
  869.                 v2 * x > (pair.ivalue - x * h2) * BASE + ztmp1.v [y-2]) {
  870.                     --x;
  871.             }
  872.             dmul(ztmp2, x, &ztmp3);
  873.             if (dsub(ztmp1, ztmp3, y, ztmp2.len))
  874.                 dadd(ztmp1, ztmp2, y, ztmp2.len);
  875.         }
  876.         ztrim(&ztmp1);
  877.     }
  878.     zshiftr(ztmp1, j);
  879.  
  880. gotanswer:
  881.     ztrim(&ztmp1);
  882.     if (ztmp2.len)
  883.         zfree(ztmp2);
  884.     if (neg && !ziszero(ztmp1)) {
  885.         zsub(z2, ztmp1, rem);
  886.         zfree(ztmp1);
  887.     } else
  888.         *rem = ztmp1;
  889. }
  890.  
  891.  
  892. /*
  893.  * Calculate the mod of an integer by a small number.
  894.  * This is only defined for positive moduli.
  895.  */
  896. long
  897. zmodi(z, n)
  898.     ZVALUE z;
  899.     long n;
  900. {
  901.     register HALF *h1;
  902.     FULL val;
  903.     HALF divval[2];
  904.     ZVALUE div;
  905.     ZVALUE temp;
  906.     long len;
  907.  
  908.     if (n == 0)
  909.         math_error("Division by zero");
  910.     if (n < 0)
  911.         math_error("Non-positive modulus");
  912.     if (ziszero(z) || (n == 1))
  913.         return 0;
  914.     if (zisone(z))
  915.         return 1;
  916.     /*
  917.      * If the modulus is by a large number, then call the normal
  918.      * modulo routine.
  919.      */
  920.     if (n & ~BASE1) {
  921.         div.sign = 0;
  922.         div.len = 2;
  923.         div.v = divval;
  924.         divval[0] = (HALF) n;
  925.         divval[1] = ((FULL) n) >> BASEB;
  926.         zmod(z, div, &temp);
  927.         n = (zistiny(temp) ? z1tol(temp) : z2tol(temp));
  928.         zfree(temp);
  929.         return n;
  930.     }
  931.     /*
  932.      * The modulus is by a small number, so we can do this quickly.
  933.      */
  934.     len = z.len;
  935.     h1 = z.v + len - 1;
  936.     val = 0;
  937.     while (len--)
  938.         val = ((val << BASEB) + ((FULL) *h1--)) % n;
  939.     if (z.sign)
  940.         val = n - val;
  941.     return val;
  942. }
  943.  
  944.  
  945. /*
  946.  * Return whether or not one number exactly divides another one.
  947.  * Returns TRUE if division occurs with no remainder.
  948.  * z1 is the number to be divided by z2.
  949.  */
  950. BOOL
  951. zdivides(z1, z2)
  952.     ZVALUE z1, z2;        /* numbers to test division into and by */
  953. {
  954.     ZVALUE temp;
  955.     long cv;
  956.  
  957.     z1.sign = 0;
  958.     z2.sign = 0;
  959.     /*
  960.      * Take care of obvious cases first
  961.      */
  962.     if (zisleone(z2)) {    /* division by zero or one */
  963.         if (*z2.v == 0)
  964.             math_error("Division by zero");
  965.         return TRUE;
  966.     }
  967.     if (ziszero(z1))    /* everything divides zero */
  968.         return TRUE;
  969.     if (z1.len < z2.len)    /* quick size comparison */
  970.         return FALSE;
  971.     if ((z1.len == z2.len) && (z1.v[z1.len-1] < z2.v[z2.len-1]))    /* more */
  972.         return FALSE;
  973.     if (zisodd(z1) && ziseven(z2))    /* can't divide odd by even */
  974.         return FALSE;
  975.     if (zlowbit(z1) < zlowbit(z2))    /* can't have smaller power of two */
  976.         return FALSE;
  977.     cv = zrel(z1, z2);    /* can't divide smaller number */
  978.     if (cv <= 0)
  979.         return (cv == 0);
  980.     /*
  981.      * Now do the real work.  Divisor divides dividend if the gcd of the
  982.      * two numbers equals the divisor.
  983.      */
  984.     zgcd(z1, z2, &temp);
  985.     cv = zcmp(z2, temp);
  986.     zfree(temp);
  987.     return (cv == 0);
  988. }
  989.  
  990.  
  991. /*
  992.  * Compute the logical OR of two numbers
  993.  */
  994. void
  995. zor(z1, z2, res)
  996.     ZVALUE z1, z2, *res;
  997. {
  998.     register HALF *sp, *dp;
  999.     long len;
  1000.     ZVALUE bz, lz, dest;
  1001.  
  1002.     if (z1.len >= z2.len) {
  1003.         bz = z1;
  1004.         lz = z2;
  1005.     } else {
  1006.         bz = z2;
  1007.         lz = z1;
  1008.     }
  1009.     dest.len = bz.len;
  1010.     dest.v = alloc(dest.len);
  1011.     dest.sign = 0;
  1012.     zcopyval(bz, dest);
  1013.     len = lz.len;
  1014.     sp = lz.v;
  1015.     dp = dest.v;
  1016.     while (len--)
  1017.         *dp++ |= *sp++;
  1018.     *res = dest;
  1019. }
  1020.  
  1021.  
  1022. /*
  1023.  * Compute the logical AND of two numbers.
  1024.  */
  1025. void
  1026. zand(z1, z2, res)
  1027.     ZVALUE z1, z2, *res;
  1028. {
  1029.     register HALF *h1, *h2, *hd;
  1030.     register long len;
  1031.     ZVALUE dest;
  1032.  
  1033.     len = ((z1.len <= z2.len) ? z1.len : z2.len);
  1034.     h1 = &z1.v[len-1];
  1035.     h2 = &z2.v[len-1];
  1036.     while ((len > 1) && ((*h1 & *h2) == 0)) {
  1037.         h1--;
  1038.         h2--;
  1039.         len--;
  1040.     }
  1041.     dest.len = len;
  1042.     dest.v = alloc(len);
  1043.     dest.sign = 0;
  1044.     h1 = z1.v;
  1045.     h2 = z2.v;
  1046.     hd = dest.v;
  1047.     while (len--)
  1048.         *hd++ = (*h1++ & *h2++);
  1049.     *res = dest;
  1050. }
  1051.  
  1052.  
  1053. /*
  1054.  * Compute the logical XOR of two numbers.
  1055.  */
  1056. void
  1057. zxor(z1, z2, res)
  1058.     ZVALUE z1, z2, *res;
  1059. {
  1060.     register HALF *sp, *dp;
  1061.     long len;
  1062.     ZVALUE bz, lz, dest;
  1063.  
  1064.     if (z1.len == z2.len) {
  1065.         for (len = z1.len; ((len > 1) && (z1.v[len-1] == z2.v[len-1])); len--) ;
  1066.         z1.len = len;
  1067.         z2.len = len;
  1068.     }
  1069.     if (z1.len >= z2.len) {
  1070.         bz = z1;
  1071.         lz = z2;
  1072.     } else {
  1073.         bz = z2;
  1074.         lz = z1;
  1075.     }
  1076.     dest.len = bz.len;
  1077.     dest.v = alloc(dest.len);
  1078.     dest.sign = 0;
  1079.     zcopyval(bz, dest);
  1080.     len = lz.len;
  1081.     sp = lz.v;
  1082.     dp = dest.v;
  1083.     while (len--)
  1084.         *dp++ ^= *sp++;
  1085.     *res = dest;
  1086. }
  1087.  
  1088.  
  1089. /*
  1090.  * Shift a number left (or right) by the specified number of bits.
  1091.  * Positive shift means to the left.  When shifting right, rightmost
  1092.  * bits are lost.  The sign of the number is preserved.
  1093.  */
  1094. void
  1095. zshift(z, n, res)
  1096.     ZVALUE z, *res;
  1097.     long n;
  1098. {
  1099.     ZVALUE ans;
  1100.     long hc;        /* number of halfwords shift is by */
  1101.  
  1102.     if (ziszero(z)) {
  1103.         *res = _zero_;
  1104.         return;
  1105.     }
  1106.     if (n == 0) {
  1107.         zcopy(z, res);
  1108.         return;
  1109.     }
  1110.     /*
  1111.      * If shift value is negative, then shift right.
  1112.      * Check for large shifts, and handle word-sized shifts quickly.
  1113.      */
  1114.     if (n < 0) {
  1115.         n = -n;
  1116.         if ((n < 0) || (n >= (z.len * BASEB))) {
  1117.             *res = _zero_;
  1118.             return;
  1119.         }
  1120.         hc = n / BASEB;
  1121.         n %= BASEB;
  1122.         z.v += hc;
  1123.         z.len -= hc;
  1124.         ans.len = z.len;
  1125.         ans.v = alloc(ans.len);
  1126.         ans.sign = z.sign;
  1127.         zcopyval(z, ans);
  1128.         if (n > 0) {
  1129.             zshiftr(ans, n);
  1130.             ztrim(&ans);
  1131.         }
  1132.         if (ziszero(ans)) {
  1133.             zfree(ans);
  1134.             ans = _zero_;
  1135.         }
  1136.         *res = ans;
  1137.         return;
  1138.     }
  1139.     /*
  1140.      * Shift value is positive, so shift leftwards.
  1141.      * Check specially for a shift of the value 1, since this is common.
  1142.      * Also handle word-sized shifts quickly.
  1143.      */
  1144.     if (zisunit(z)) {
  1145.         zbitvalue(n, res);
  1146.         res->sign = z.sign;
  1147.         return;
  1148.     }
  1149.     hc = n / BASEB;
  1150.     n %= BASEB;
  1151.     ans.len = z.len + hc + 1;
  1152.     ans.v = alloc(ans.len);
  1153.     ans.sign = z.sign;
  1154.     if (hc > 0)
  1155.         memset((char *) ans.v, 0, hc * sizeof(HALF));
  1156.     memcpy((char *) (ans.v + hc), 
  1157.         (char *) z.v, z.len * sizeof(HALF));
  1158.     ans.v[ans.len - 1] = 0;
  1159.     if (n > 0) {
  1160.         ans.v += hc;
  1161.         ans.len -= hc;
  1162.         zshiftl(ans, n);
  1163.         ans.v -= hc;
  1164.         ans.len += hc;
  1165.     }
  1166.     ztrim(&ans);
  1167.     *res = ans;
  1168. }
  1169.  
  1170.  
  1171. /*
  1172.  * Return the position of the lowest bit which is set in the binary
  1173.  * representation of a number (counting from zero).  This is the highest
  1174.  * power of two which evenly divides the number.
  1175.  */
  1176. long
  1177. zlowbit(z)
  1178.     ZVALUE z;
  1179. {
  1180.     register HALF *zp;
  1181.     long n;
  1182.     HALF dataval;
  1183.     HALF *bitval;
  1184.  
  1185.     n = 0;
  1186.     for (zp = z.v; *zp == 0; zp++)
  1187.         if (++n >= z.len)
  1188.             return 0;
  1189.     dataval = *zp;
  1190.     bitval = bitmask;
  1191.     while ((*(bitval++) & dataval) == 0) {
  1192.     }
  1193.     return (n*BASEB)+(bitval-bitmask-1);
  1194. }
  1195.  
  1196.  
  1197. /*
  1198.  * Return the position of the highest bit which is set in the binary
  1199.  * representation of a number (counting from zero).  This is the highest power
  1200.  * of two which is less than or equal to the number (which is assumed nonzero).
  1201.  */
  1202. long
  1203. zhighbit(z)
  1204.     ZVALUE z;
  1205. {
  1206.     HALF dataval;
  1207.     HALF *bitval;
  1208.  
  1209.     dataval = z.v[z.len-1];
  1210.     bitval = bitmask+BASEB;
  1211.     if (dataval) {
  1212.         while ((*(--bitval) & dataval) == 0) {
  1213.         }
  1214.     }
  1215.     return (z.len*BASEB)+(bitval-bitmask-BASEB);
  1216. }
  1217.  
  1218.  
  1219. #if 0
  1220. /*
  1221.  * Reverse the bits of a particular range of bits of a number.
  1222.  *
  1223.  * This function returns an integer with bits a thru b swapped.
  1224.  * That is, bit a is swapped with bit b, bit a+1 is swapped with b-1,
  1225.  * and so on.
  1226.  *
  1227.  * As a special case, if the ending bit position is < 0, is it taken to 
  1228.  * mean the highest bit set.  Thus zbitrev(0, -1, z, &res) will 
  1229.  * perform a complete bit reverse of the number 'z'.
  1230.  *
  1231.  * As a special case, if the starting bit position is < 0, is it taken to 
  1232.  * mean the lowest bit set.  Thus zbitrev(-1, -1, z, &res) is the
  1233.  * same as zbitrev(lowbit(z), highbit(z), z, &res).
  1234.  *
  1235.  * Note that the low order bit number is taken to be 0.  Also, bitrev
  1236.  * ignores the sign of the number.
  1237.  *
  1238.  * Bits beyond the highest bit are taken to be zero.  Thus the calling
  1239.  * bitrev(0, 100, _one_, &res) will result in a value of 2^100.
  1240.  */
  1241. void
  1242. zbitrev(low, high, z, res)
  1243.     long low;    /* lowest bit to reverse, <0 => lowbit(z) */
  1244.     long high;    /* highest bit to reverse, <0 => highbit(z) */
  1245.     ZVALUE z;    /* value to bit reverse */
  1246.     ZVALUE *res;    /* resulting bit reverse number */
  1247. {
  1248. }
  1249. #endif
  1250.  
  1251.  
  1252. /*
  1253.  * Return whether or not the specifed bit number is set in a number.
  1254.  * Rightmost bit of a number is bit 0.
  1255.  */
  1256. BOOL
  1257. zisset(z, n)
  1258.     ZVALUE z;
  1259.     long n;
  1260. {
  1261.     if ((n < 0) || ((n / BASEB) >= z.len))
  1262.         return FALSE;
  1263.     return ((z.v[n / BASEB] & (((HALF) 1) << (n % BASEB))) != 0);
  1264. }
  1265.  
  1266.  
  1267. /*
  1268.  * Check whether or not a number has exactly one bit set, and
  1269.  * thus is an exact power of two.  Returns TRUE if so.
  1270.  */
  1271. BOOL
  1272. zisonebit(z)
  1273.     ZVALUE z;
  1274. {
  1275.     register HALF *hp;
  1276.     register LEN len;
  1277.  
  1278.     if (ziszero(z) || zisneg(z))
  1279.         return FALSE;
  1280.     hp = z.v;
  1281.     len = z.len;
  1282.     while (len > 4) {
  1283.         len -= 4;
  1284.         if (*hp++ || *hp++ || *hp++ || *hp++)
  1285.             return FALSE;
  1286.     }
  1287.     while (--len > 0) {
  1288.         if (*hp++)
  1289.             return FALSE;
  1290.     }
  1291.     return ((*hp & -*hp) == *hp);        /* NEEDS 2'S COMPLEMENT */
  1292. }
  1293.  
  1294.  
  1295. /*
  1296.  * Check whether or not a number has all of its bits set below some
  1297.  * bit position, and thus is one less than an exact power of two.
  1298.  * Returns TRUE if so.
  1299.  */
  1300. BOOL
  1301. zisallbits(z)
  1302.     ZVALUE z;
  1303. {
  1304.     register HALF *hp;
  1305.     register LEN len;
  1306.     HALF digit;
  1307.  
  1308.     if (ziszero(z) || zisneg(z))
  1309.         return FALSE;
  1310.     hp = z.v;
  1311.     len = z.len;
  1312.     while (len > 4) {
  1313.         len -= 4;
  1314.         if ((*hp++ != BASE1) || (*hp++ != BASE1) ||
  1315.             (*hp++ != BASE1) || (*hp++ != BASE1))
  1316.                 return FALSE;
  1317.     }
  1318.     while (--len > 0) {
  1319.         if (*hp++ != BASE1)
  1320.             return FALSE;
  1321.     }
  1322.     digit = (HALF)(*hp + 1);
  1323.     return ((digit & -digit) == digit);    /* NEEDS 2'S COMPLEMENT */
  1324. }
  1325.  
  1326.  
  1327. /*
  1328.  * Return the number whose binary representation contains only one bit which
  1329.  * is in the specified position (counting from zero).  This is equivilant
  1330.  * to raising two to the given power.
  1331.  */
  1332. void
  1333. zbitvalue(n, res)
  1334.     long n;
  1335.     ZVALUE *res;
  1336. {
  1337.     ZVALUE z;
  1338.  
  1339.     if (n < 0) n = 0;
  1340.     z.sign = 0;
  1341.     z.len = (n / BASEB) + 1;
  1342.     z.v = alloc(z.len);
  1343.     zclearval(z);
  1344.     z.v[z.len-1] = (((HALF) 1) << (n % BASEB));
  1345.     *res = z;
  1346. }
  1347.  
  1348.  
  1349. /*
  1350.  * Compare a number against zero.
  1351.  * Returns the sgn function of the number (-1, 0, or 1).
  1352.  */
  1353. FLAG
  1354. ztest(z)
  1355.     ZVALUE z;
  1356. {
  1357.     register int sign;
  1358.     register HALF *h;
  1359.     register long len;
  1360.  
  1361.     sign = 1;
  1362.     if (z.sign)
  1363.         sign = -sign;
  1364.     h = z.v;
  1365.     len = z.len;
  1366.     while (len--)
  1367.         if (*h++)
  1368.             return sign;
  1369.     return 0;
  1370. }
  1371.  
  1372.  
  1373. /*
  1374.  * Compare two numbers to see which is larger.
  1375.  * Returns -1 if first number is smaller, 0 if they are equal, and 1 if
  1376.  * first number is larger.  This is the same result as ztest(z2-z1).
  1377.  */
  1378. FLAG
  1379. zrel(z1, z2)
  1380.     ZVALUE z1, z2;
  1381. {
  1382.     register HALF *h1, *h2;
  1383.     register long len1, len2;
  1384.     int sign;
  1385.  
  1386.     sign = 1;
  1387.     if (z1.sign < z2.sign)
  1388.         return 1;
  1389.     if (z2.sign < z1.sign)
  1390.         return -1;
  1391.     if (z2.sign)
  1392.         sign = -1;
  1393.     len1 = z1.len;
  1394.     len2 = z2.len;
  1395.     h1 = z1.v + z1.len - 1;
  1396.     h2 = z2.v + z2.len - 1;
  1397.     while (len1 > len2) {
  1398.         if (*h1--)
  1399.             return sign;
  1400.         len1--;
  1401.     }
  1402.     while (len2 > len1) {
  1403.         if (*h2--)
  1404.             return -sign;
  1405.         len2--;
  1406.     }
  1407.     while (len1--) {
  1408.         if (*h1-- != *h2--)
  1409.             break;
  1410.     }
  1411.     if ((len1 = *++h1) > (len2 = *++h2))
  1412.         return sign;
  1413.     if (len1 < len2)
  1414.         return -sign;
  1415.     return 0;
  1416. }
  1417.  
  1418.  
  1419. /*
  1420.  * Compare two numbers to see if they are equal or not.
  1421.  * Returns TRUE if they differ.
  1422.  */
  1423. BOOL
  1424. zcmp(z1, z2)
  1425.     ZVALUE z1, z2;
  1426. {
  1427.     register HALF *h1, *h2;
  1428.     register long len;
  1429.  
  1430.     if ((z1.sign != z2.sign) || (z1.len != z2.len) || (*z1.v != *z2.v))
  1431.         return TRUE;
  1432.     len = z1.len;
  1433.     h1 = z1.v;
  1434.     h2 = z2.v;
  1435.     while (len-- > 0) {
  1436.         if (*h1++ != *h2++)
  1437.             return TRUE;
  1438.     }
  1439.     return FALSE;
  1440. }
  1441.  
  1442.  
  1443. /*
  1444.  * Internal utility subroutines
  1445.  */
  1446. static void
  1447. dadd(z1, z2, y, n)
  1448.     ZVALUE z1, z2;
  1449.     long y, n;
  1450. {
  1451.     HALF *s1, *s2;
  1452.     short carry;
  1453.     long sum;
  1454.  
  1455.     s1 = z1.v + y - n;
  1456.     s2 = z2.v;
  1457.     carry = 0;
  1458.     while (n--) {
  1459.         sum = (long)*s1 + (long)*s2 + carry;
  1460.         carry = 0;
  1461.         if (sum >= BASE) {
  1462.             sum -= BASE;
  1463.             carry = 1;
  1464.         }
  1465.         *s1 = (HALF)sum;
  1466.         ++s1;
  1467.         ++s2;
  1468.     }
  1469.     sum = (long)*s1 + carry;
  1470.     *s1 = (HALF)sum;
  1471. }
  1472.  
  1473.  
  1474. /*
  1475.  * Do subtract for divide, returning TRUE if subtraction went negative.
  1476.  */
  1477. static BOOL
  1478. dsub(z1, z2, y, n)
  1479.     ZVALUE z1, z2;
  1480.     long y, n;
  1481. {
  1482.     HALF *s1, *s2, *s3;
  1483.     FULL i1;
  1484.     BOOL neg;
  1485.  
  1486.     neg = FALSE;
  1487.     s1 = z1.v + y - n;
  1488.     s2 = z2.v;
  1489.     if (++n > z2.len)
  1490.         n = z2.len;
  1491.     while (n--) {
  1492.         i1 = (FULL) *s1;
  1493.         if (i1 < (FULL) *s2) {
  1494.             s3 = s1 + 1;
  1495.             while (s3 < z1.v + z1.len && !(*s3)) {
  1496.                 *s3 = BASE1;
  1497.                 ++s3;
  1498.             }
  1499.             if (s3 >= z1.v + z1.len)
  1500.                 neg = TRUE;
  1501.             else
  1502.                 --(*s3);
  1503.             i1 += BASE;
  1504.         }
  1505.         *s1 = i1 - (FULL) *s2;
  1506.         ++s1;
  1507.         ++s2;
  1508.     }
  1509.     return neg;
  1510. }
  1511.  
  1512.  
  1513. /*
  1514.  * Multiply a number by a single 'digit'.
  1515.  * This is meant to be used only by the divide routine, and so the
  1516.  * destination area must already be allocated and be large enough.
  1517.  */
  1518. static void
  1519. dmul(z, mul, dest)
  1520.     ZVALUE z;
  1521.     FULL mul;
  1522.     ZVALUE *dest;
  1523. {
  1524.     register HALF *zp, *dp;
  1525.     SIUNION sival;
  1526.     FULL carry;
  1527.     long len;
  1528.  
  1529.     dp = dest->v;
  1530.     dest->sign = 0;
  1531.     if (mul == 0) {
  1532.         dest->len = 1;
  1533.         *dp = 0;
  1534.         return;
  1535.     }
  1536.     len = z.len;
  1537.     zp = z.v + len - 1;
  1538.     while ((*zp == 0) && (len > 1)) {
  1539.         len--;
  1540.         zp--;
  1541.     }
  1542.     dest->len = len;
  1543.     zp = z.v;
  1544.     carry = 0;
  1545.     while (len >= 4) {
  1546.         len -= 4;
  1547.         sival.ivalue = (mul * ((FULL) *zp++)) + carry;
  1548.         *dp++ = sival.silow;
  1549.         sival.ivalue = (mul * ((FULL) *zp++)) + ((FULL) sival.sihigh);
  1550.         *dp++ = sival.silow;
  1551.         sival.ivalue = (mul * ((FULL) *zp++)) + ((FULL) sival.sihigh);
  1552.         *dp++ = sival.silow;
  1553.         sival.ivalue = (mul * ((FULL) *zp++)) + ((FULL) sival.sihigh);
  1554.         *dp++ = sival.silow;
  1555.         carry = sival.sihigh;
  1556.     }
  1557.     while (--len >= 0) {
  1558.         sival.ivalue = (mul * ((FULL) *zp++)) + carry;
  1559.         *dp++ = sival.silow;
  1560.         carry = sival.sihigh;
  1561.     }
  1562.     if (carry) {
  1563.         *dp = (HALF)carry;
  1564.         dest->len++;
  1565.     }
  1566. }
  1567.  
  1568.  
  1569. /*
  1570.  * Utility to calculate the gcd of two small integers.
  1571.  */
  1572. long
  1573. iigcd(i1, i2)
  1574.     long i1, i2;
  1575. {
  1576.     FULL f1, f2, temp;
  1577.  
  1578.     f1 = (FULL) ((i1 >= 0) ? i1 : -i1);
  1579.     f2 = (FULL) ((i2 >= 0) ? i2 : -i2);
  1580.     while (f1) {
  1581.         temp = f2 % f1;
  1582.         f2 = f1;
  1583.         f1 = temp;
  1584.     }
  1585.     return (long) f2;
  1586. }
  1587.  
  1588.  
  1589. void
  1590. ztrim(z)
  1591.     ZVALUE *z;
  1592. {
  1593.     register HALF *h;
  1594.     register long len;
  1595.  
  1596.     h = z->v + z->len - 1;
  1597.     len = z->len;
  1598.     while (*h == 0 && len > 1) {
  1599.         --h;
  1600.         --len;
  1601.     }
  1602.     z->len = len;
  1603. }
  1604.  
  1605.  
  1606. /*
  1607.  * Utility routine to shift right.
  1608.  */
  1609. void
  1610. zshiftr(z, n)
  1611.     ZVALUE z;
  1612.     long n;
  1613. {
  1614.     register HALF *h, *lim;
  1615.     FULL mask, maskt;
  1616.     long len;
  1617.  
  1618.     if (n >= BASEB) {
  1619.         len = n / BASEB;
  1620.         h = z.v;
  1621.         lim = z.v + z.len - len;
  1622.         while (h < lim) {
  1623.             h[0] = h[len];
  1624.             ++h;
  1625.         }
  1626.         n -= BASEB * len;
  1627.         lim = z.v + z.len;
  1628.         while (h < lim)
  1629.             *h++ = 0;
  1630.     }
  1631.     if (n) {
  1632.         len = z.len;
  1633.         h = z.v + len - 1;
  1634.         mask = 0;
  1635.         while (len--) {
  1636.             maskt = (((FULL) *h) << (BASEB - n)) & BASE1;
  1637.             *h = (*h >> n) | mask;
  1638.             mask = maskt;
  1639.             --h;
  1640.         }
  1641.     }
  1642. }
  1643.  
  1644.  
  1645. /*
  1646.  * Utility routine to shift left.
  1647.  */
  1648. void
  1649. zshiftl(z, n)
  1650.     ZVALUE z;
  1651.     long n;
  1652. {
  1653.     register HALF *h;
  1654.     FULL mask, i;
  1655.     long len;
  1656.  
  1657.     if (n >= BASEB) {
  1658.         len = n / BASEB;
  1659.         h = z.v + z.len - 1;
  1660.         while (!*h)
  1661.             --h;
  1662.         while (h >= z.v) {
  1663.             h[len] = h[0];
  1664.             --h;
  1665.         }
  1666.         n -= BASEB * len;
  1667.         while (len)
  1668.             h[len--] = 0;
  1669.     }
  1670.     if (n > 0) {
  1671.         len = z.len;
  1672.         h = z.v;
  1673.         mask = 0;
  1674.         while (len--) {
  1675.             i = (((FULL) *h) << n) | mask;
  1676.             if (i > BASE1) {
  1677.                 mask = i >> BASEB;
  1678.                 i &= BASE1;
  1679.             } else
  1680.                 mask = 0;
  1681.             *h = (HALF) i;
  1682.             ++h;
  1683.         }
  1684.     }
  1685. }
  1686.  
  1687. /*
  1688.  * initmasks - init the bitmask rotation arrays
  1689.  *
  1690.  * bitmask[i]      (1 << (i-1)),  for  -BASEB*4<=i<=BASEB*4
  1691.  *
  1692.  * The bmask array contains 8 cycles of rotations of a bit mask.
  1693.  */
  1694. void
  1695. initmasks()
  1696. {
  1697.     int i;
  1698.  
  1699.     /*
  1700.      * setup the bmask array
  1701.      */
  1702.     bmask = alloc((long)((8*BASEB)+1));
  1703.     for (i=0; i < (8*BASEB)+1; ++i) {
  1704.         bmask[i] = 1 << (i%BASEB);
  1705.     }
  1706.  
  1707.     /*
  1708.      * setup the rmask pointers
  1709.      */
  1710.     rmask = (HALF **)malloc(sizeof(HALF *)*((BASEB*4)+2));
  1711.     for (i = 0; i <= (4*BASEB)+1; ++i) {
  1712.         rmask[i] = &bmask[(2*BASEB)+i];
  1713.     }
  1714.  
  1715.     /*
  1716.      * setup the bitmask array to allow -4*BASEB thru 4*BASEB indexing
  1717.      */
  1718.     bitmask = &bmask[4*BASEB];
  1719.     return;
  1720. }
  1721.  
  1722. /* END CODE */
  1723.